package org.android.SuperList;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.AdapterView.AdapterContextMenuInfo;

public class FormasPagos extends ListActivity
{
 
    private DbAdapter mDbHelper;
    
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.formaspago);
        mDbHelper= new DbAdapter(this);
        mDbHelper.open();
        CargarDatos();
        registerForContextMenu(getListView());
        
    }
    

	public void CargarDatos()
    {
    	Cursor FormasPagosCursor=  mDbHelper.RecuperaRegistrosTabla(DbAdapter.TABLA_FORMASPAGO);
    	startManagingCursor(FormasPagosCursor);    	
		String[] Origen = new String[]{DbAdapter.FORMA_PAGO};
		int [] Destino = new int[]{R.id.txvformaspago};    	
		SimpleCursorAdapter lista = new SimpleCursorAdapter(this,R.layout.registroformaspago,FormasPagosCursor,Origen,Destino);		
		setListAdapter(lista);
    }
	
	@Override
    public boolean onCreateOptionsMenu(Menu menu) 
    {
        super.onCreateOptionsMenu(menu);
        menu.add(0, clsGeneral.MENU_INSERTAR, 0, R.string.menu_insertar);       
        return true;        
    }
    
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) 
    {
		super.onCreateContextMenu(menu, v, menuInfo);
        menu.add(0,clsGeneral.MENU_ELIMINAR, 0, R.string.menu_eliminar);        
        menu.add(0,clsGeneral.MENU_CANCELAR, 1, R.string.menu_cancelar);
	}
    
    @Override
    public boolean onContextItemSelected(MenuItem item)
    {
    	AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
    	switch(item.getItemId())
    	{
    		case clsGeneral.MENU_ELIMINAR:
    		try
    		{
    			mDbHelper.eliminaRegistro(DbAdapter.TABLA_FORMASPAGO,info.id);
    		}
    		catch(Exception E)
    		{
    			clsGeneral.showAlerts(clsGeneral.LBL_ERROR,clsGeneral.MSG_ERRORINTEGRIDAD,this);
    		}
    			CargarDatos();
    			return true;
    	}
    	return super.onContextItemSelected(item);
    }
    
   
    @Override
	public boolean onMenuItemSelected(int caracteristica,MenuItem item)
    {
    	switch(item.getItemId())
    	{
    	case clsGeneral.MENU_INSERTAR:
    		creaFormaPago();
    		return true;    	
    	}	
    	return super.onMenuItemSelected(caracteristica, item);
    } 
    
    
    public void creaFormaPago()
    {
    	Intent i= new Intent(this,EditaFormasPagos.class);
    	startActivityForResult(i,clsGeneral.ACTIVITY_CREAR);    	
    }
    

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) 
    {
    	super.onListItemClick(l, v, position, id);
        Intent i = new Intent(this,EditaFormasPagos.class);
        i.putExtra(DbAdapter.ID, id);
        startActivityForResult(i,clsGeneral.ACTIVITY_DETALLE);
    }
    
    	
}
